package com.jack.helloworld.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jack.helloworld.R;
import com.jack.helloworld.bean.User;

import java.util.ArrayList;
import java.util.List;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
	public static List<User> users = new ArrayList<User>() {
		{
			add(new User(R.drawable.icon, "刘备", "唯贤唯德，能服于人"));
			add(new User(R.drawable.icon, "诸葛亮", "淡泊以明志，宁静以致远"));
			add(new User(R.drawable.icon, "关羽", "安能与老兵同列"));
			add(new User(R.drawable.icon, "赵云", "子龙一身是胆"));
			add(new User(R.drawable.icon, "曹操", "宁教我负天下人，不教天下人负我"));
			add(new User(R.drawable.icon, "司马懿", "老而不死是为贼"));
			add(new User(R.drawable.icon, "司马昭", "司马昭之心路人皆知"));
			add(new User(R.drawable.icon, "孙权", "生子当如孙仲谋"));
			add(new User(R.drawable.icon, "周瑜", "既生瑜何生亮"));
			add(new User(R.drawable.icon, "吕蒙", "士别三日当刮目相待"));
		}
	};
	
	public static SparseArray<String> groups = new SparseArray<>();
	
	static {
		groups.put(0, "蜀国");
		groups.put(1, "魏国");
		groups.put(2, "吴国");
	}
	
	private Context context;
	private LayoutInflater inflater;
	
	public RecyclerAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}
	
	@Override
	public RecyclerViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
		View itemView = inflater.inflate(R.layout.user_item, parent, false);
		return new RecyclerViewHolder(itemView);
	}
	
	@Override
	public void onBindViewHolder(RecyclerViewHolder holder, int position) {
		holder.bindView(users.get(position));
	}
	
	@Override
	public int getItemCount() {
		return users.size();
	}
	
	
	public List<?> getData() {
		return users;
	}
	
	
	class RecyclerViewHolder extends RecyclerView.ViewHolder {
		ImageView portrait;
		TextView name;
		TextView desc;
		
		public RecyclerViewHolder(View itemView) {
			super(itemView);
			
			name = itemView.findViewById(R.id.name);
			desc = itemView.findViewById(R.id.desc);
			portrait = itemView.findViewById(R.id.portrait);
		}
		
		
		public void bindView(final User user) {
			name.setText(user.getName());
			desc.setText(user.getDesc());
			portrait.setImageResource(user.getPortrait());
		}
	}
}